//
//  NavigationController.m
//  gdemm
//
//  Created by 乔明超 on 16/9/20.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "NavigationController.h"
#import "Masonry.h"
#import "Utils.h"
#import "UIAlertController+alert.h"
@interface NavigationController ()
@property (strong,nonatomic) UIImageView *navigationImageView;

@end

@implementation NavigationController

+(void)initialize{
    NSDictionary *textAtt = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:[DeviceTypeJudge getTitleFont]]};
    [[UINavigationBar appearance]setTitleTextAttributes:textAtt];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:[Utils colorWithKey:@"mainBgColor"]];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(skinSwitchHandler) name:@"skinswitch" object:nil];
    self.navigationBar.translucent = NO;
    UIImageView *navigationImageView = [self findHairlineImageViewUnder:self.navigationBar];
    self.navigationImageView = navigationImageView;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.cover = [[UIView alloc]init];
    self.cover.backgroundColor = [Utils colorWithKey:@"mainLineColor"];
    [self.navigationImageView addSubview:self.cover];
    [self.cover mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.mas_equalTo(0);
    }];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}
-(UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

/**
 *  肤色切换
 */

- (void)skinSwitchHandler{
    [[UINavigationBar appearance] setBarTintColor:[Utils colorWithKey:@"mainBgColor"]];
    self.cover.backgroundColor = [Utils colorWithKey:@"mainLineColor"];
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIViewController *)popViewControllerAnimated:(BOOL)animated{
    NSArray * viewControllers = self.viewControllers;
    UIViewController * vc = viewControllers[viewControllers.count - 2];
    if([viewControllers.lastObject isKindOfClass:NSClassFromString(@"DwgViewController")]){
        [UIAlertController exchangeMethod];
        UIWindow * window = [UIApplication sharedApplication].keyWindow;
        [window.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.tag == 10000 || obj.tag == 10001 || obj.tag == 10002) {
                [obj removeFromSuperview];
            }
            
        }];
        
    }
    [super popViewControllerAnimated:YES];
    return vc;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
